const express = require("express")
const router = express.Router()
const db = require("../../sqlServer/dbConnect")

const time = require("../../utils/time")

// 食品生产-- 查询
router.post("/product", (req, res) => {
    const { name,pageConfig,doName } = req.body;
    const {page,pageSize} = pageConfig
    let sql = name === undefined ? "select * from product limit ?,?;select * from product" : "select * from product where name=?"
    const data = name === undefined ? [(Number(page)-1)*Number(pageSize),Number(pageSize)] : [name]
    db.sqlQuery(sql,data,(err, result) => {
        if (err) {
            console.log(err)
            res.send({
                // data: result,
                msg: "获取生产信息失败",
                status: 203
            })
        }else {
            name === undefined ?
            res.send({
                data: result[0],
                total:result[1].length,
                msg: "获取生产信息成功",
                status: 200
            }) :
            res.send({
                data: result,
                total:result.length,
                msg: "获取生产信息成功",
                status: 200
            })
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"食品生产查询",time.getData()])
})

// 食品生产-- 添加
router.post("/product/add", (req, res) => {
    const { name,environment,period,amount,qgp,sId,doName } = req.body
    const date = time.getData()
    let sql = "insert into product(name,environment, period,amount,date,qgp,sId) values(?,?,?,?,?,?,?)"
    const data = [name,environment,period,amount,date,qgp,sId]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "添加失败",
                status: 203
            })
        } else {
            res.send({
                msg: "添加成功",
                status: 200
            })
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"食品生产添加",time.getData()])
})

// 食品生产-- 编辑
router.post("/product/edit", (req, res) => {
    const { name,environment,period,amount,date,qgp,sId, Id,doName } = req.body
    let sql = "update product set name=?,environment=?,period=?,amount=?,date=?,qgp=?,sId=? where Id=?"
    const data = [name,environment,period,amount,date,qgp,sId, Id]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "编辑失败",
                status: 203
            })
        } else {
            res.send({
                msg: "编辑成功",
                status: 200
            })
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"食品生产编辑",time.getData()])
})

// 食品生产-- 删除
router.post("/product/delete", (req, res) => {
    const { Id,doName } = req.body
    let sql = "delete from product where Id=?"
    const data = [Id]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "删除失败",
                status: 203
            })
        } else {
            res.send({
                msg: "删除成功",
                status: 200
            })
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"食品生产删除",time.getData()])
})

module.exports = router